﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace swfupload
{
    /// <summary>
    /// UploadFile 的摘要说明
    /// </summary>
    public class UploadFiles : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            HttpPostedFile file = null;
            try
            {
                file = context.Request.Files["FileData"];
            }
            catch (Exception mye)
            {
                context.Response.Write("-1|" + mye.Message.ToString());
                return;
            }
            if (file != null)
            {
                UploadFile uf = new UploadFile();
                UploadFileExt ufe = GetFilePath();
                //上传的类别，如上传用户头像type=upload_avatar
                string strType = HttpContext.Current.Request.QueryString["type"];
                string[] arr = strType.Split('|');
                int intWidth = 0, intHeight = 0;
                if (arr.Length >= 3)
                {
                    if (arr[1] != "" && arr[2] != "")
                    {
                        intWidth = Convert.ToInt32(arr[1]);
                        intHeight = Convert.ToInt32(arr[2]);

                    }
                }
                //-----------ftp上传图片------
                //上传文件的指定路径+年月
                string strPicturePath = string.Format("{0}{1}/", ufe.Path, DateTime.Now.ToString("yyyyMM"));
                //文件的命名规则
                string strFileName = DateTime.Now.ToFileTime().ToString() + "" + new Random().Next(1000, 9999).ToString();



                string strPath = "";//上传成功后返回的相对路径
                if (uf.UploadFTP(file, ufe.Ext, ufe.Size, intWidth, intHeight, strPicturePath, strFileName, ref strPath))
                {
                    context.Response.Write(string.Format("1|{0}", System.Configuration.ConfigurationManager.AppSettings["website_resource"] + strPath));//域名+相对路径
                }
                else
                {
                    context.Response.Write("0|" + strPath);
                }
            }
            else
            {
                context.Response.Write("0|error");
            }
        }


        /// <summary>
        /// 获取上传文件的扩展信息
        /// </summary>
        /// <returns></returns>
        private UploadFileExt GetFilePath()
        {
            //上传文件的类型 如上传图像采用 type= upload_avatar
            string type = HttpContext.Current.Request.QueryString["type"];
            string[] arr = type.Split('|');
            UploadFileExt data = new UploadFileExt();
            if (!string.IsNullOrWhiteSpace(arr[0]))
            {
                string strPath = System.Configuration.ConfigurationManager.AppSettings[type + "_path"];
                string strExt = System.Configuration.ConfigurationManager.AppSettings[type + "_ext"];
                int intSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings[type + "_size"]);
                if (!string.IsNullOrWhiteSpace(strPath) && !string.IsNullOrWhiteSpace(strExt) && intSize > 0)
                {
                    data.Path = strPath;
                    data.Ext = strExt;
                    data.Size = intSize;
                }
            }
            return data;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

    //默认的基本信息
    class UploadFileExt
    {
        private string _path = "/Images/", _ext = ".jpg;.gif;.png";
        private int _size = 200000;
        public string Path { get { return _path; } set { _path = value; } }
        public string Ext { get { return _ext; } set { _ext = value; } }
        public int Size { get { return _size; } set { _size = value; } }
    }

}